<template>
  <div class="layui-layout layui-layout-admin">
    <div class="adminnav1">
      <Pagetop></Pagetop>
    </div>
    <div class="adminnav2">
      <Secondnav urlname="shopmall"></Secondnav>
    </div>

    <!-- 主体内容 -->
    <div class="layui-body kedou-admin" id="LAY_app_body app">
      <div class="layadmin-tabsbody-item layui-show">
        <div class="pagelist">
          <div class="layui-card-body">
            <fieldset class="layui-elem-field layui-field-title">
              <legend>商品列表</legend>
            </fieldset>
          </div>
          <div class="pagetop">
            <Search :seadate="searchArr"></Search>
          </div>
          <div class="layui-card-body">
            <div class="buttop">
              <button
                type="button"
                class="layui-btn  layui-btn-normal"
                @click="addData"
              >
                添加商品
              </button>
              <!-- <button type="button" class="layui-btn  layui-btn-warm" @click="shopfz">商品分组</button>-->
            </div>
            <Tables
              :fdate="pdate"
              :result="res"
              height="430"
              :totalCount="totalRows"
              :pageNumber="curindex"
              :fbtn="btn"
            ></Tables>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
//	import echarts from 'echarts'
import Pagetop from "../../common/pagetop.vue";
import Secondnav from "../../common/secondnav.vue";
import Tables from "../../common/tables.vue";
import Search from "../../common/search.vue";
export default {
  name: "ucshopmallList",
  components: { Pagetop, Secondnav, Tables, Search },
  data() {
    return {
      pdate: [], //表头
      res: "", //内容
      totalRows: "", //总数量
      curindex: 1, //页数
      btn: [], //按钮
      searchArr: [], //搜索
      isShow: false,
      seapram: ""
    };
  },
  mounted() {
    this.getdate();
    this.getsearch();
  },
  methods: {
    getdate: function() {
      var _this = this;
      var type = localStorage.getItem("type");
      var url = "/mallbean/bapi/v1.9/item";
      url=dourl(url)
      if (_this.seapram) {
        var dates = {
          size: pageSize,
          page: _this.curindex
          //sort:'createAt,DESC',
          //'status.equals':'ENABLE'
        };
        var res = _this.seapram;
        for (var i = 0; i < res.length; i++) {
          dates[res[i].key] = res[i].val;
        }
      } else {
        var dates = {
          size: pageSize,
          page: _this.curindex
          //sort:'createAt,DESC',
          //'status.equals':'ENABLE'
        };
      }
      // alert(JSON.stringify(dates))
      // alert(url + "//" + JSON.stringify(dates))
      ajaxRequest(url, "GET", "json", dates, function(ret, err) {
        //alert(url+"//"+JSON.stringify(ret))
        var arr = [];
        arr[0] = [
          { field: "id", title: "商品Id" },
          { field: "name", title: "商品名称" },
          {
            field: "logoUrl",
            title: "商品图",
            templet:
              '<div><img src="{{d.logoUrl}}" style="display: inline-block; height: 100%;"></div>'
          },
          {
            field: "activeStatus",
            title: "商品状态",
            templet: function(d) {
              if (d.activeStatus == "ENABLE") return "正常销售";
              else if (d.activeStatus == "DISABLE") return "下架脱销";
              else return "删除";
            }
          },
          {
            field: "saleStock",
            title: "销量",
            templet: function(d) {
              return d.saleStock.saleAmount;
            }
          },
          {
            field: "saleStock",
            title: "库存",
            templet: function(d) {
              return d.saleStock.stockAmount;
            }
          },
          {
            field: "price",
            title: "商品单价（元）",
            templet: function(d) {
              return d.price.specialPrice/100;
            }
          },
          {
            field: "right",
            title: "操作",
            width: "200",
            fixed: "right",
            templet: function(d) {
              var str1 =
                '<a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>';
              var str =
                '<a class="layui-btn layui-btn-xs layui-btn-normal" lay-event="detail">详情</a>';
              var str2 =
                '<a class="layui-btn layui-btn-xs  layui-btn-warm" lay-event="freego">规格管理</a>';
              if (type == 1) {
              	if (d.activeStatus == "ENABLE"){
              		var str4='<a class="layui-btn layui-btn-xs  layui-btn-warm" lay-event="upper">下架</a>';
              	}
              	else{
              		var str4='<a class="layui-btn layui-btn-xs  layui-btn-edit" lay-event="upper">上架</a>';
              	}
             
              	return  str4;
              }
              else{
              	return str1;
              }
            }
          }
        ];
        _this.pdate = arr;
        _this.res = ret.data.list; //res1;
        _this.totalRows = ret.data.totalRows;
        _this.curindex = ret.data.page + 1;
      });
    },
    getsearch: function() {
      var arr = [
        { field: "name", title: "商品名称", type: "input" }
        //  		,{field: 'status', title: '商品状态',type: 'select',option:[{val:'-1',name:'全部'},{val:'1',name:'正常销售'},{val:'2',name:'下架'}]}
        //
      ];
      this.searchArr = arr;
    },
    page: function(index, obj) {
      //alert(JSON,stringify(obj))
      this.curindex = index;
      this.getdate();
    },
    sousuo: function(pram) {
      // alert(JSON.stringify(pram))
      this.seapram = pram;
      this.res = "";
      this.totalRows = "";
      this.curindex = 1;
      this.getdate();
    },
    addData: function() {
      // this.$router.push({path:'/shopmalladd'})
      var _this = this;
      layer.open({
        type: 2,
        title: "新增商品",
        content: baseurl + "/ucshopmalladd",
        area: ["70%", "80%"]
      });
    },
    editInfo: function(data) {
      //this.$router.push({name:'shopmalladd',query:{'shopId':data.shopId,'commodityId':data.commodityId}})
      var _this = this;
      layer.open({
        type: 2,
        title: "编辑商品",
        content: baseurl + "/ucshopmalladd",
        area: ["70%", "80%"],
        success: function(layero, index) {
          var iframeWin = window[layero.find("iframe")[0]["name"]];
          iframeWin.getdates(data);
        }
      });
    },
    //	shopfz:function(){
    //		//this.$router.push({path:'/shopmalldr'})
    //	},
    detailInfo: function(data) {
      var _this = this;
      var id = data.id;
      layer.open({
        type: 2,
        title: "查看详情",
        shadeClose: false, //点击遮罩关闭
        content: baseurl + "ucshopmalldetail",
        area: ["70%", "80%"],
        success: function(layero, index) {
          var iframeWin = window[layero.find("iframe")[0]["name"]];
          iframeWin.getdates(data);
        }
      });
    },
    upperInfo: function(data) {
      var _this = this;
      //alert('上下架')
      if (data.activeStatus == 'ENABLE') {
        var status = 'DISABLE';
      } else {
        var status = 'ENABLE';
      }
      var data = {
       "activeStatus": status,
  		 "id":data.id
      };
      //alert(JSON.stringify(data))
      var url = "/mallbean/aapi/v1.9/item/updateItemActiveStatus";
      ajaxRequest(url, 'PUT', "json", data, function(ret, err) {
        layer.msg("操作成功");
        setTimeout(function() {
         
          _this.getdate();
        }, 800);
      });
    },
    retailInfo: function(data) {
      var _this = this;
      if (data.needToDistribute == 0) {
        var needToDistribute = 1;
      } else {
        var needToDistribute = 0;
      }
      var data = {
        needToDistribute: needToDistribute,
        commodityId: data.commodityId,
        shopId: data.shopId
      };
      var url = "/shopCommodity/updateNeedToDistributeOfShopCommodity";
      ajaxRequest(url, "GET", "json", data, function(ret, err) {
        layer.msg("操作成功");
        setTimeout(function() {
          _this.res = "";
          _this.curindex = 1;
          _this.getdate();
        }, 800);
      });
    },
    freegoInfo: function(data) {
      //this.$router.push({name:'gglist',query:{'shopId':data.shopId,'goodsId':data.goodsId}})
      var _this = this;
      //var id=data.id;
      layer.open({
        type: 2,
        title: "",
        shadeClose: false, //点击遮罩关闭
        content: baseurl + "/ucgglist",
        area: ["80%", "80%"],
        success: function(layero, index) {
          var iframeWin = window[layero.find("iframe")[0]["name"]];
          iframeWin.getdates(data);
        }
      });
    },
    screens: function() {
      return screen();
    }
  },
  updated: function() {
    this.isShow = true;
    layui.use("form", function() {
      var form = layui.form;
      form.render();
    });
  }
};
</script>
<style scoped>
/**/
</style>
